class JsChannel {
  constructor() {
    this.communicator =
      window.ReactNativeWebView ||
      window.native ||
      webkit.messageHandlers.native;

    // send for downloading html
    this.send = {};
  }

  postMessage(action, data) {
    return new Promise((resolve, reject) => {
      const _uuid = uuid();
      this.send[_uuid] = {
        resolve: data => {
          delete this.send[_uuid];
          resolve(data);
        },
        reject: error => {
          delete this.send[_uuid];
          reject(error);
        }
      };

      const jsonString = JSON.stringify({
        action: action,
        uuid: _uuid,
        data: data || {}
      });
      this.communicator.postMessage(jsonString);
    });
  }
}

const jsChannel = new JsChannel();
